package mspbots.data.tl.prod.quickbooks.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.prod.quickbooks.mapper.QuickbooksAccountMapper;
import mspbots.data.tl.prod.quickbooks.model.QuickbooksAccountEntity;
import mspbots.data.tl.prod.quickbooks.service.QuickbooksAccountService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

/**
 * @author Jay.Yang
 */

@Service
@Log4j2
@DS("master")
public class QuickbooksAccountServiceImpl extends AbstractBaseServiceImpl<QuickbooksAccountMapper, QuickbooksAccountEntity>
        implements QuickbooksAccountService {

    @Override
    public void sync(JsonNode node) {
        try{
            log.debug("................[qbd.account.tl] MQ START.......");
            if (ObjectUtils.isEmpty(node.get("tenantId")) || ObjectUtils.isEmpty(node.get("ListID"))){
                log.error("[qbd.account.tl] sync error,tenantId or ListID is null!");
                return;
            }
            QuickbooksAccountEntity accountEntity = QuickbooksAccountEntity.builder()
                    .isActive(ObjectUtils.isEmpty(node.get("IsActive")) ? null : node.get("IsActive").asBoolean())
                    .name(ObjectUtils.isEmpty(node.get("Name")) ? null : node.get("Name").asText())
                    .accountType(ObjectUtils.isEmpty(node.get("AccountType")) ? null : node.get("AccountType").asText())
                    .subLevel(ObjectUtils.isEmpty(node.get("Sublevel")) ? null : node.get("Sublevel").asInt())
                    .listId(ObjectUtils.isEmpty(node.get("ListID")) ? null : node.get("ListID").asText())
                    .accountDesc(ObjectUtils.isEmpty(node.get("Desc")) ? null : node.get("Desc").asText())
                    .accountNumber(ObjectUtils.isEmpty(node.get("AccountNumber")) ? null : node.get("AccountNumber").asInt())
                    .syncTime(LocalDateTime.now())
                    .build();
            accountEntity.setTenantCode(node.get("tenantId").asLong());
            if (!ObjectUtils.isEmpty(node.get("ParentRef"))){
                accountEntity.setParentListId(node.get("ParentRef").get("ListID").asText());
            }

            QuickbooksAccountEntity dbData = super.baseMapper.selectOne(Wrappers.<QuickbooksAccountEntity>lambdaQuery()
                    .eq(QuickbooksAccountEntity::getTenantCode, accountEntity.getTenantCode())
                    .eq(QuickbooksAccountEntity::getListId, accountEntity.getListId())
                    .last("limit 1"));

            if (ObjectUtils.isEmpty(dbData)){
                super.baseMapper.insert(accountEntity);
            }else{
                accountEntity.setId(dbData.getId());
                super.baseMapper.updateById(accountEntity);
            }
        }catch (Exception e){
            log.error("QuickbooksAccountServiceImpl sync error: {}",e.getMessage());
        }

    }
}
